package services.common;



import models.common.entity.t_company_datum;
import models.common.entity.t_company_info_side;
import services.base.BaseService;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhongan.scorpoin.common.ZhongAnOpenException;
import common.utils.BusinessExample;
import common.utils.Factory;
import common.utils.HTMLSpirit;
import common.utils.ResultInfo;

import daos.common.CompanyDatumDao;

/**
 * 企业认证信息service
 * 
 *@author binghui.zhou
 *@createDate 2017年6月15日 上午11:51:15
 */
public class CompanyDatumService extends BaseService<t_company_datum>{
	
	protected static CompanyDatumDao companyDatumDao= Factory.getDao(CompanyDatumDao.class);
	
	public CompanyDatumService(){
		super.basedao=this.companyDatumDao;
	}
	
	/**
	 * 保存企业认证资料
	 * 
	 * @param datum 
	 * @return
	 * 
	 * @author binghui.zhou
	 * @createDate 2017年6月15日 上午11:51:15
	 */
	public boolean saveCompanyDatume(t_company_datum datum){
		return companyDatumDao.save(datum);
	}
	
	/***
	 * 根据用户ID查找企业认证信息
	 * @param userId
	 * @return
	 * @author binghui.zhou
	 * @createDate 2017年6月15日
	 */
	public t_company_datum findByDatum(long userId) {
		return companyDatumDao.findByColumn(" user_id=?", userId);
	}
	/**
	 * 添加、查询企业认证信息
	 *
	 * @param infoSide
	 * @return
	 * @throws ZhongAnOpenException
	 * @author xiaojun
	 *@version 2017-9-22
	 */
	public ResultInfo authenticate(t_company_info_side infoSide ) throws ZhongAnOpenException{
		ResultInfo result = new ResultInfo();
		// 查找企业信息是否存在
		t_company_datum companyDatum = findByDatum(infoSide.user_id);

		if (companyDatum != null) {
			result.code = 1;
			result.msg = "查询成功";
			result.obj = companyDatum;
			return result;
		} else {
			// 查找企业认证信息
			JSONObject object = BusinessExample.zaeds(infoSide.full_name);
//			JSONObject object = BusinessExample.resolution(infoSide.full_name);
	
			if (null != object && object.size() > 0) {
	
				t_company_datum datum = new t_company_datum();
				datum.user_id = infoSide.user_id;
				datum.address = object.get("address").toString();
				datum.company_name = object.get("name").toString();
				datum.legal_person = object.get("operName").toString();
	//					.replace("\"", "").replace("[", "").replace("]", "");
				datum.reg_capital = object.get("registCapi").toString();
				datum.reg_code = object.get("creditNo").toString();
				datum.reg_date = object.get("startDate").toString();
				datum.shareholder = object.get("partners").toString();
				JSONArray json = (JSONArray) object.get("partners");
				for (int i = 0;i<json.size();i++) {
					JSONObject jsonObject = (JSONObject) json.get(i);
					datum.shareholder +=jsonObject.get("name").toString();
					if(i != json.size()-1 && jsonObject.get("name").toString() != null && !jsonObject.get("name").toString().equals("")){
						datum.shareholder+=",";
					}
				}
				datum.state = object.get("status").toString();
				// 保存企业认证资料
				boolean bln = saveCompanyDatume(datum);
				if (!bln) {
					result.code = -1;
					result.msg = "企业数据插入失败";
				
	
					return result;
				}
	
				result.code = 1;
				result.msg = "查询成功";
				result.obj = datum;
				return result;
	
			} else {
				result.code = -1;
				result.msg = "未查询到企业认证资料, 请核对企业全称";
				return result;
			}
		}
	}
	/**
	 * 更新企业认证信息
	 *
	 * @param infoSide
	 * @return
	 * @throws ZhongAnOpenException
	 * @author xiaojun
	 *@version 2017-9-22
	 */
	public ResultInfo updateAuthenticate(t_company_info_side infoSide ) throws ZhongAnOpenException{
		ResultInfo result = new ResultInfo();
		// 查找企业信息是否存在
		t_company_datum datum = findByDatum(infoSide.user_id);

		if (datum == null) {
			 datum = new t_company_datum();
		} 
		// 查找企业认证信息
		JSONObject object = BusinessExample.zaeds(infoSide.full_name);
//		JSONObject object = BusinessExample.resolution(infoSide.full_name);

		if (null != object && object.size() > 0) {
			datum.user_id = infoSide.user_id;
			datum.address = HTMLSpirit.delHTMLTag(object.get("address").toString()).replaceAll("-->", "").trim();
			datum.company_name = object.get("name").toString();
			datum.legal_person = object.get("operName").toString();
//					.replace("\"", "").replace("[", "").replace("]", "");
			datum.reg_capital = object.get("registCapi").toString();
			datum.reg_code = object.get("creditNo").toString();
			if(!infoSide.business_license_code.equals(datum.reg_code)){
				result.code = -1;
				result.msg = "请填写正确的统一社会信用代码！";
				return result;
			}
			datum.reg_date = object.get("startDate").toString();
			datum.shareholder = "";
			JSONArray json = (JSONArray) object.get("partners");
			for (int i = 0;i<json.size();i++) {
				JSONObject jsonObject = (JSONObject) json.get(i);
				datum.shareholder +=jsonObject.get("name").toString();
				if(i != json.size()-1 && jsonObject.get("name").toString() != null && !jsonObject.get("name").toString().equals("")){
					datum.shareholder+=",";
				}
			}
			datum.state = object.get("status").toString();
			// 保存企业认证资料
			boolean bln = saveCompanyDatume(datum);
			if (!bln) {
				result.code = -1;
				result.msg = "企业数据插入失败";
				return result;
			}
			result.code = 1;
			result.msg = "更新成功";
			result.obj = datum;
			return result;

		} else {
			result.code = -1;
			result.msg = "未查询到企业认证资料, 请核对企业全称";
			return result;
		}
		
	}
	/**
	 * 根据userId查企业认证信息
	 * 
	 * @param user_id
	 * @return
	 * @author zhiliang.lan
	 * @createDate 2017年6月19日
	 */
	public t_company_datum findByUserId(long user_id) {
		
		return companyDatumDao.findByUserId(user_id);
	}
	
}
